package com.ruoyi.graph.dao;

import com.ruoyi.graph.domain.Relationship;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;

import java.util.List;

public interface RelationshipDao extends Neo4jRepository<Relationship,Long> {

    @Query("MATCH p=(a)-[r:RELATIONSHIP*0..]->(b) where a.tableName={0} and a.database={1}  return p" +
            " union all " +
            "MATCH p=(a)<-[r:RELATIONSHIP*0..]-(b) where a.tableName={0} and a.database={1}  return p")
    List<Relationship> findAllRelationStream(String tableName, String database);

    @Query("MATCH (n:TableNode{tableName:{0},database: {1}}),(m:TableNode{tableName:{2},database: {3}}) CREATE (n)-[r:RELATIONSHIP{name:{4}}]->(m) RETURN r")
    public Relationship createRelationShip(String startTableName, String startDatabase, String endTableName, String endDatabase, String relationName);

    @Query("MATCH (n:`TableNode`),(m:`TableNode`) WHERE id(n)={0} AND id(m) = {1}}  CREATE (n)-[r:RELATIONSHIP{name:{2}}}]->(m) RETURN r")
    public Relationship createRelationShip(long startId, long endId, String relationName);

    @Query("match(n:TableNode {tableName:{0},database:{1}}) -[t:RELATIONSHIP{name:{2}}]-(m:TableNode{tableName:{3},database:{4}})  detach delete t")
    public void deleteRelationship(String sourceTableName,String sourceDatabase,String relationName,String targetTableName,String targetDatabase);

    @Query("match(n:TableNode {id:{0}}} -[t:RELATIONSHIP]-(m:TableNode{id:{1}}) detach delete t")
    public void deleteRelationship(long startId,long endId);
}
